package com.example.thirdhomework

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.thirdhomework.databinding.ActivityMainBinding

const val KEY1 = "key1"
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        binding.button2.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java)
            intent.putExtra(KEY1, binding.editTextText.text.toString())
            startActivity(intent)
        }
        val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val str = result.data?.getStringExtra(KEY1)
                binding.editTextText.setText(str)
            }
        }
        binding.button4.setOnClickListener{
            val intent = Intent(this, SecondActivity::class.java)
            launcher.launch(intent)
        }
        val launcher2 = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            binding.imageView.setImageBitmap(result.data?.extras?.get("data") as Bitmap)
        }
        binding.button5.setOnClickListener {
            val intent = Intent("android.media.action.IMAGE_CAPTURE")
            launcher2.launch(intent)
        }
        binding.Thirdbutton.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }
    }
}